﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SimulationSettings.CSISSimulationSettingsManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Runtime.InteropServices;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.SimulationSettings;

internal class CSISSimulationSettingsManager
{
  private double _orginalTimeInterval;
  private static CSISSimulationSettingsManager _instance = new CSISSimulationSettingsManager();

  [DllImport("UiTasks.dll")]
  private static extern double GetSimulationTimeInterval();

  [DllImport("UiTasks.dll")]
  private static extern void SetSimulationTimeInterval(double newTime);

  protected CSISSimulationSettingsManager() => this._orginalTimeInterval = -1.0;

  public static bool CanChangeTimeInterval
  {
    get => CSISSimulationSettingsManager._instance._orginalTimeInterval < 0.0;
  }

  public static double SimulationTimeInterval
  {
    get => CSISSimulationSettingsManager.GetSimulationTimeInterval();
    set => CSISSimulationSettingsManager.SetSimulationTimeInterval(value);
  }

  public static void SetTemporaryTimeInterval(double timeInterval)
  {
    if (!CSISSimulationSettingsManager._instance.TimeIntervalIsDiffentFromCurrentSimTimeInterval(timeInterval) || TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning())
      return;
    CSISSimulationSettingsManager._instance._orginalTimeInterval = CSISSimulationSettingsManager.SimulationTimeInterval;
    CSISSimulationSettingsManager.SimulationTimeInterval = timeInterval;
  }

  public static void ResetTemporaryTimeInterval()
  {
    if (CSISSimulationSettingsManager._instance._orginalTimeInterval <= 0.0)
      return;
    if (TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning())
      TxApplication.ActiveDocument.SimulationPlayer.Pause();
    CSISSimulationSettingsManager.SimulationTimeInterval = CSISSimulationSettingsManager._instance._orginalTimeInterval;
    CSISSimulationSettingsManager._instance._orginalTimeInterval = -1.0;
  }

  private bool TimeIntervalIsDiffentFromCurrentSimTimeInterval(double newTimeInterval)
  {
    double simulationTimeInterval = CSISSimulationSettingsManager.SimulationTimeInterval;
    int afterDecimalPoint = TxApplication.Options.Units.TimeDigitsAfterDecimalPoint;
    return Math.Round(simulationTimeInterval, afterDecimalPoint) != Math.Round(newTimeInterval, afterDecimalPoint);
  }
}
